/**
 * Toast 通知工具模块
 * 提供自定义的提示消息功能，替代原生 alert
 */
class ToastManager {
    constructor() {
        this.container = null;
        this.init();
    }

    init() {
        // 创建 toast 容器
        this.container = document.createElement('div');
        this.container.id = 'toastContainer';
        this.container.style.cssText = `
            position: fixed;
            top: 20px;
            right: 20px;
            z-index: 10000;
            display: flex;
            flex-direction: column;
            gap: 10px;
        `;
        document.body.appendChild(this.container);
    }

    /**
     * 显示 toast 消息
     * @param {string} message - 消息内容
     * @param {string} type - 消息类型 (success, error, warning, info)
     * @param {number} duration - 显示时长（毫秒）
     */
    show(message, type = 'info', duration = 3000) {
        const toast = document.createElement('div');
        toast.className = `toast toast-${type}`;
        toast.style.cssText = `
            padding: 16px 20px;
            border-radius: 8px;
            color: white;
            font-size: 14px;
            font-weight: 500;
            box-shadow: 0 6px 20px rgba(0,0,0,0.15);
            transform: translateX(100%);
            opacity: 0;
            transition: all 0.3s ease;
            max-width: 380px;
            min-width: 320px;
            word-wrap: break-word;
        `;

        // 根据类型设置样式
        const typeStyles = {
            success: 'background: linear-gradient(135deg, #a8e6cf, #88d8a3); color: #2d5a3d;',
            error: 'background: linear-gradient(135deg, #ffb3ba, #ff9aa2); color: #8b2635;',
            warning: 'background: linear-gradient(135deg, #ffd3a5, #fd9853); color: #8b4513;',
            info: 'background: linear-gradient(135deg, #a8d8ea, #7fb3d3); color: #2c5282;'
        };
        toast.style.cssText += typeStyles[type] || typeStyles.info;

        toast.textContent = message;
        this.container.appendChild(toast);

        // 触发动画
        setTimeout(() => {
            toast.style.transform = 'translateX(0)';
            toast.style.opacity = '1';
        }, 10);

        // 自动移除（duration为0时不自动移除）
        if (duration > 0) {
            setTimeout(() => {
                toast.style.transform = 'translateX(100%)';
                toast.style.opacity = '0';
                setTimeout(() => {
                    if (toast.parentNode) {
                        toast.parentNode.removeChild(toast);
                    }
                }, 300);
            }, duration);
        }

        return toast;
    }

    /**
     * 显示成功消息
     */
    success(message, duration = 3000) {
        return this.show(message, 'success', duration);
    }

    /**
     * 显示错误消息
     */
    error(message, duration = 5000) {
        return this.show(message, 'error', duration);
    }

    /**
     * 显示警告消息
     */
    warning(message, duration = 4000) {
        return this.show(message, 'warning', duration);
    }

    /**
     * 显示信息消息
     */
    info(message, duration = 3000) {
        return this.show(message, 'info', duration);
    }

    /**
     * 显示确认对话框
     * @param {string} message - 确认消息
     * @param {Function} onConfirm - 确认回调
     * @param {Function} onCancel - 取消回调
     */
    confirm(message, onConfirm, onCancel) {
        const confirmToast = document.createElement('div');
        confirmToast.className = 'toast-confirm';
        confirmToast.style.cssText = `
            padding: 24px;
            border-radius: 12px;
            background: linear-gradient(135deg, #ffffff 0%, #f8fafc 100%);
            color: #2d3748;
            font-size: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.15);
            transform: translateX(100%);
            transition: all 0.3s ease;
            max-width: 380px;
            border: 1px solid rgba(226, 232, 240, 0.8);
            backdrop-filter: blur(10px);
        `;

        confirmToast.innerHTML = `
            <div style="margin-bottom: 20px; font-weight: 600; line-height: 1.5; text-align: center;">
                ${message}
            </div>
            <div style="display: flex; gap: 12px; justify-content: center;">
                <button id="toast-cancel" style="
                    padding: 10px 20px;
                    border: none;
                    background: linear-gradient(135deg, #a0aec0, #718096);
                    color: white;
                    border-radius: 8px;
                    cursor: pointer;
                    font-size: 14px;
                    font-weight: 600;
                    transition: all 0.2s ease;
                    box-shadow: 0 2px 8px rgba(160, 174, 192, 0.3);
                    min-width: 80px;
                ">取消</button>
                <button id="toast-confirm" style="
                    padding: 10px 20px;
                    border: none;
                    background: linear-gradient(135deg, #4CAF50, #45a049);
                    color: white;
                    border-radius: 8px;
                    cursor: pointer;
                    font-size: 14px;
                    font-weight: 600;
                    transition: all 0.2s ease;
                    box-shadow: 0 2px 8px rgba(76, 175, 80, 0.3);
                    min-width: 80px;
                ">确认</button>
            </div>
        `;

        this.container.appendChild(confirmToast);

        // 触发动画
        setTimeout(() => {
            confirmToast.style.transform = 'translateX(0)';
        }, 10);

        // 添加悬停效果
        const confirmBtn = confirmToast.querySelector('#toast-confirm');
        const cancelBtn = confirmToast.querySelector('#toast-cancel');
        
        confirmBtn.addEventListener('mouseenter', () => {
            confirmBtn.style.transform = 'translateY(-1px)';
            confirmBtn.style.boxShadow = '0 4px 12px rgba(76, 175, 80, 0.4)';
        });
        confirmBtn.addEventListener('mouseleave', () => {
            confirmBtn.style.transform = 'translateY(0)';
            confirmBtn.style.boxShadow = '0 2px 8px rgba(76, 175, 80, 0.3)';
        });
        
        cancelBtn.addEventListener('mouseenter', () => {
            cancelBtn.style.transform = 'translateY(-1px)';
            cancelBtn.style.boxShadow = '0 4px 12px rgba(160, 174, 192, 0.4)';
        });
        cancelBtn.addEventListener('mouseleave', () => {
            cancelBtn.style.transform = 'translateY(0)';
            cancelBtn.style.boxShadow = '0 2px 8px rgba(160, 174, 192, 0.3)';
        });

        // 绑定事件
        confirmBtn.onclick = () => {
            confirmToast.style.transform = 'translateX(100%)';
            setTimeout(() => {
                if (confirmToast.parentNode) {
                    confirmToast.parentNode.removeChild(confirmToast);
                }
            }, 300);
            if (onConfirm) onConfirm();
        };

        cancelBtn.onclick = () => {
            confirmToast.style.transform = 'translateX(100%)';
            setTimeout(() => {
                if (confirmToast.parentNode) {
                    confirmToast.parentNode.removeChild(confirmToast);
                }
            }, 300);
            if (onCancel) onCancel();
        };

        return confirmToast;
    }

    /**
     * 显示输入对话框
     * @param {string} message - 提示消息
     * @param {string} placeholder - 输入框占位符
     * @param {Function} onConfirm - 确认回调，参数为输入值
     * @param {Function} onCancel - 取消回调
     */
    prompt(message, placeholder = '', onConfirm, onCancel) {
        const promptToast = document.createElement('div');
        promptToast.className = 'toast-prompt';
        promptToast.style.cssText = `
            padding: 20px;
            border-radius: 8px;
            background: white;
            color: #333;
            font-size: 14px;
            box-shadow: 0 8px 24px rgba(0,0,0,0.2);
            transform: translateX(100%);
            transition: all 0.3s ease;
            max-width: 350px;
            border: 1px solid #ddd;
        `;

        promptToast.innerHTML = `
            <div style="margin-bottom: 15px;">${message}</div>
            <input type="text" id="toast-input" placeholder="${placeholder}" style="
                width: 100%;
                padding: 8px 12px;
                border: 1px solid #ddd;
                border-radius: 4px;
                font-size: 13px;
                margin-bottom: 15px;
                box-sizing: border-box;
            " />
            <div style="display: flex; gap: 10px; justify-content: flex-end;">
                <button id="toast-cancel" style="
                    padding: 6px 16px;
                    border: 1px solid #ddd;
                    background: #f8f9fa;
                    border-radius: 4px;
                    cursor: pointer;
                    font-size: 13px;
                ">取消</button>
                <button id="toast-confirm" style="
                    padding: 6px 16px;
                    border: none;
                    background: #007bff;
                    color: white;
                    border-radius: 4px;
                    cursor: pointer;
                    font-size: 13px;
                ">确定</button>
            </div>
        `;

        this.container.appendChild(promptToast);

        // 触发动画
        setTimeout(() => {
            promptToast.style.transform = 'translateX(0)';
            promptToast.querySelector('#toast-input').focus();
        }, 10);

        // 绑定事件
        const input = promptToast.querySelector('#toast-input');
        const confirmBtn = promptToast.querySelector('#toast-confirm');
        const cancelBtn = promptToast.querySelector('#toast-cancel');

        const close = () => {
            promptToast.style.transform = 'translateX(100%)';
            setTimeout(() => {
                if (promptToast.parentNode) {
                    promptToast.parentNode.removeChild(promptToast);
                }
            }, 300);
        };

        confirmBtn.onclick = () => {
            const value = input.value.trim();
            close();
            if (onConfirm) onConfirm(value);
        };

        cancelBtn.onclick = () => {
            close();
            if (onCancel) onCancel();
        };

        input.onkeypress = (e) => {
            if (e.key === 'Enter') {
                confirmBtn.click();
            }
        };

        return promptToast;
    }
}

// 创建全局实例
window.toast = new ToastManager();

// 导出类（如果使用模块系统）
if (typeof module !== 'undefined' && module.exports) {
    module.exports = ToastManager;
}
